package org.tpdata.api.singlequality.hospitalqc;

import asserts.Asserts;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.google.common.base.Objects;
import common.CaseInfo;
import common.Data;
import common.RunCaseJson;
import common.SetUpTearDown;
import exceptions.TestNGRetry;
import io.qameta.allure.Allure;
import io.qameta.allure.Feature;
import io.qameta.allure.Story;
import io.restassured.RestAssured;
import io.restassured.response.Response;
import org.apache.log4j.Logger;
import org.testng.Assert;
import org.testng.annotations.DataProvider;
import org.testng.annotations.Test;
import utils.Contants;
import utils.ExcelUtils;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import static common.TestStep.requestAndRespondBody;

@Feature("获取年度质控工作趋势")
public class YearQCTrend extends SetUpTearDown {
    Logger log = Logger.getLogger(YearQCTrend.class);
    Data data = envSetUp();
    @Story("获取年度质控工作趋势")
    @Test(retryAnalyzer = TestNGRetry.class, dataProvider = "datas")
    public void runCase(CaseInfo caseInfo) {
        Allure.description(caseInfo.getMedCode()+caseInfo.getDesc());
        JSONObject json = new JSONObject();
//        Map<String,String > json=new HashMap<String, String>();
        json.put("access_token", data.getAccessToken());
        json.put("authToken", data.getAccessToken());
        json.put("uniqu_no", data.getUniquNo());
        json.put("operatorId", data.getUserId());
        json.put("authUserId", data.getUserId());
        json.put("create_id", data.getUserId());
        json.put("reportNos", caseInfo.getReportNos());     //  报告号，多个值以逗号连接  值01002
        //nzkt0005 个人年度完成的质控工作趋势
        json.put("medCode", caseInfo.getMedCode());
//        json.put("indexTimeStart", caseInfo.getIndexTimeStart());
//        json.put("indexTimeEnd", caseInfo.getIndexTimeEnd());

        log.info(json.toString());
        Response response = RunCaseJson.runCase(json, "get", caseInfo.getUri());
        String jsonStr = response.asString();
        log.info("响应报文：" + jsonStr);

        JSONObject obj = JSONObject.parseObject(jsonStr);
        JSONArray contentArr = obj.getJSONArray("responseData");
        String url = RestAssured.baseURI + ":" + RestAssured.port + caseInfo.getUri();
        requestAndRespondBody(url, json, jsonStr);
        boolean flag = Asserts.VerityValue(contentArr);
        Assert.assertEquals(flag, Contants.SUCCESS);
    }

    @DataProvider
    public Object[] datas() {
        List<CaseInfo> list = ExcelUtils.read(this.sheetIndex, 1, CaseInfo.class);
        List lt = new ArrayList();
        String StrName=this.getClass().getSimpleName();
            for (CaseInfo caseInfo : list) {
                if (Objects.equal(caseInfo.getClassName(),StrName)  ) {
                    lt.add(caseInfo);
                }
            }

        return lt.toArray();

    }

}
